package com.bit.cfdi.web.vm.negocio;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;

import com.bit.cfdi.web.domain.Giro;
import com.bit.cfdi.web.services.GiroService;
import com.bit.cfdi.web.services.NegocioGiroService;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class ModalWindowGiro1ViewModel {	
		
	private Integer indexTabbox;
	private String textTitle;
	private Logger log = LogManager.getLogger(this.getClass().getName());
	@WireVariable
	private GiroService giroService;	
	private List<WrapperGiros> listaGiros;	
	private List<WrapperGiros> listGirosSelected;	
	private List<WrapperGiros> listGirosReceived;
	private Map<String, List<WrapperGiros>> mapGirosReceived;		
	private String radioSelected;		
	
	@SuppressWarnings("unchecked")
	@Init
	public void init()
	{	
		listaGiros = new ArrayList<WrapperGiros>();
		//Recibiendo la lista almacenada y mostrada en los textbox
		mapGirosReceived = new HashMap<String, List<WrapperGiros>>();
		mapGirosReceived = (Map<String, List<WrapperGiros>>) Executions.getCurrent().getArg();
		listGirosReceived = mapGirosReceived.get("listGiros");
		radioSelected = null;
		indexTabbox = 0;
		textTitle = "Selecciona los Giros que tu Negocio utilizará";
		loadGiros(listGirosReceived);
	}

	public Integer getIndexTabbox() {
		return indexTabbox;
	}

	public void setIndexTabbox(Integer indexTabbox) {
		this.indexTabbox = indexTabbox;
	}
	
	public String getTextTitle() {
		return textTitle;
	}
	
	public List<WrapperGiros> getListaGiros() {
		return listaGiros;
	}
	
	public List<WrapperGiros> getListaGirosSelected() {
		return listGirosSelected;
	}	
		
	public String getRadioSelected() {
		return radioSelected;
	}		

	private Boolean showGirosSelected()
	{			
		listGirosSelected = new ArrayList<WrapperGiros>();
		for(WrapperGiros currentGiro : listaGiros)
		{
			if(currentGiro.getStatus())
			{
				listGirosSelected.add(new WrapperGiros(currentGiro.getGiro(),
						currentGiro.getNombreGiro(), currentGiro.getStatus()));
			}
		}
		if(listaGiros.isEmpty() || listGirosSelected.isEmpty())
		{
			return false;
		}			
		changeStatusGirosSelected();		
		return true;
	}
	
	private void changeStatusGirosSelected()
	{
		for(WrapperGiros currentGiro: listGirosSelected)
		{
			currentGiro.setStatus(false);
		}
	}
	
	@Command
	@NotifyChange({"indexTabbox", "textTitle", 
		"listaGirosSelected"})
	public void goModalWindowGiros2()
	{
		if(showGirosSelected())
		{
			indexTabbox = 1;
			textTitle = "¿Qué Giro se usará como predeterminado?";
		}						
	}
	
	@Command
	@NotifyChange({"indexTabbox", "textTitle", 
		"listaGirosSelected"})
	public void goModalWindowGiros1()
	{
		indexTabbox = 0;		
		textTitle = "Selecciona los Giros que tu Negocio utilizará";
		radioSelected = null;
	}	
	
	@NotifyChange({"listaGiros"})
	public void loadGiros(List<WrapperGiros> listGirosReceived)
	{				
		List<Giro> listG = giroService.getAll();
		if(listG.isEmpty())
		{
			return;
		}		
		for(Giro currentGiro : listG)
		{						
			WrapperGiros objWrapper  = new WrapperGiros();			
			objWrapper.setGiro(currentGiro);
			objWrapper.setNombreGiro(currentGiro.getDescripcion());
			objWrapper.setStatus(false);
			if(listGirosReceived != null)
			{
				for(WrapperGiros currentGiroReceived : listGirosReceived)
				{
					if(currentGiro.getIdGiro()
							.equals(currentGiroReceived.getGiro().getIdGiro()))
					{
						objWrapper.setStatus(true);
						break;
					}
				}
			}			
			listaGiros.add(objWrapper);
		}					
	}	
	
	@Command
	@NotifyChange({"visibleWindow"})
	public void saveGiroPredetermined()
	{		
		if(radioSelected != null)
		{
			log.info("Radio Selected ---> " + radioSelected);
			for(WrapperGiros current : listGirosSelected)
			{
				if(current.getNombreGiro().equals(radioSelected))
				{
					current.setStatus(true);
				}
			}
			Map <String, Object> objGiro = new HashMap<String, Object>();
			objGiro.put("listGiros", listGirosSelected);
			BindUtils.postGlobalCommand(null, null, "processGiros", objGiro);
			
			BindUtils.postGlobalCommand(null, null, "wizardHideGiroPopUp", null);
		}				
	}		
	
	@Command
	public void showRadioSelected(@BindingParam("rdSelected") String rdSelected)
	{		
		radioSelected = rdSelected;
	}						
	
}
